import axios from 'axios'

const apiClient = axios.create({
  baseURL: '/api/v1',
  timeout: 10000,
})

// 请求拦截器
apiClient.interceptors.request.use(
  (config) => {
    // 可以在这里添加认证token
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

export const api = {
  // 健康检查
  healthCheck: () => apiClient.get('/health'),

  // 连接器相关
  getConnectors: () => apiClient.get('/connectors'),
  createConnector: (data: any) => apiClient.post('/connectors', data),
  getConnector: (id: string) => apiClient.get(`/connectors/${id}`),
  updateConnector: (id: string, data: any) => apiClient.put(`/connectors/${id}`, data),
  deleteConnector: (id: string) => apiClient.delete(`/connectors/${id}`),
  testConnector: (id: string) => apiClient.post(`/connectors/${id}/test`),

  // 连接相关
  getConnections: () => apiClient.get('/connections'),
  createConnection: (data: any) => apiClient.post('/connections', data),
  getConnection: (id: string) => apiClient.get(`/connections/${id}`),
  updateConnection: (id: string, data: any) => apiClient.put(`/connections/${id}`, data),
  deleteConnection: (id: string) => apiClient.delete(`/connections/${id}`),
  startConnection: (id: string) => apiClient.post(`/connections/${id}/start`),
  stopConnection: (id: string) => apiClient.post(`/connections/${id}/stop`),

  // 管道相关
  getPipelines: () => apiClient.get('/pipelines'),
  createPipeline: (data: any) => apiClient.post('/pipelines', data),
  getPipeline: (id: string) => apiClient.get(`/pipelines/${id}`),
  getPipelineMetrics: (id: string) => apiClient.get(`/pipelines/${id}/metrics`),

  // 主题相关
  getTopics: () => apiClient.get('/topics'),
  createTopic: (data: any) => apiClient.post('/topics', data),
  deleteTopic: (name: string) => apiClient.delete(`/topics/${name}`),
  getTopicRecords: (name: string) => apiClient.get(`/topics/${name}/records`),

  // AI助手相关
  suggestConfiguration: (data: any) => apiClient.post('/ai/suggest', data),
  analyzeDataQuality: (data: any) => apiClient.post('/ai/analyze', data),
}
